package com.xw.childhealth.controller;

import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.dao.BasicDataDao;
import com.xw.childhealth.entity.BasicData;
import com.xw.childhealth.util.ImageUtil;
import com.xw.childhealth.util.PathUtil;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-06 08:43
 * @Version: 1.0
 */
//@RestController

@RequestMapping("/test")
@Controller
public class TestController {

    @Autowired
    private BasicDataDao dao;

//    @PostMapping("/hello")
//    public ModelAndView hello(Map<String, Object> map) {
//        return new ModelAndView("pages/admin/index", map);
//    }
//    @GetMapping("/hello2")
//    public ModelAndView hello2(Map<String, Object> map) {
//        return new ModelAndView("pages/admin/article-list", map);
//    }


    @PostMapping("/testFileUpload")
    @ResponseBody
    public ResultVO testFileUpload(BasicData a, MultipartFile file, HttpServletRequest request) {
        System.out.println(file);
        addShopImg(a, file);
        //1添加对象
        //存储图片

        return ResultVOUtil.success();
    }

    private void addShopImg(BasicData a, MultipartFile shopImg) {
        // 获取shop图片目录的相对值路径
        String dest = PathUtil.getImgPath(a.getCsrq());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest);
        a.setTx(shopImgAddr);
        dao.save(a);

    }


}
